package utility;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Timestamp;

import javax.rmi.CORBA.Util;

import core.NetProtocoll;

public class Utility {

	public static Timestamp getTimestamp()
	{
		  java.util.Date today = new java.util.Date();
		    return new java.sql.Timestamp(today.getTime());
	}

	public static void sendSocketMessage(String msg, ObjectOutputStream out)
	{
		try{
			out.writeObject(msg);
			out.flush();
			
			System.out.println("CLIENT-> Invio: " + msg);
		}
		catch(IOException ioException){
			System.out.println("Server>Errore durante l'invio del messaggio al client");
		}
	}

	public static String readSocketMessage(ObjectInputStream in)
	{
		try{
			String message;
			message = (String)in.readObject();
			
			System.out.println("CLIENT-> Ricevo: " + message);
			
			return message;
		} catch(IOException e) {
			System.out.println(Utility.getTimestamp() + ": Errore durante la ricezione del messaggio");
			return NetProtocoll.ERROR;
		} catch (ClassNotFoundException e) {
			System.out.println("Classe non trovata!");
			return NetProtocoll.ERROR;		
		}
	}

}